#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class TeacherClassProvider : CY.GFive.Core.Providers.DALProvider.ITeacherClassProvider
	{
		#region Sql
		private static readonly string SqlSelectTeacherClass = "SELECT [Id], [TeacherCode],[ClassCode],[IsState] FROM [TeacherClass] WHERE  [Id]=@Id";

		private static readonly string SqlInsertTeacherClass = "INSERT INTO [TeacherClass]([TeacherCode],[ClassCode],[IsState]) values(@TeacherCode,@ClassCode,@IsState);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateTeacherClass = "UPDATE [TeacherClass] SET [TeacherCode] = @TeacherCode,[ClassCode] = @ClassCode,[IsState] = @IsState WHERE  [Id]=@Id";

		private static readonly string SqlDeleteTeacherClass = "DELETE [TeacherClass] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllTeacherClass = "SELECT [Id], [TeacherCode],[ClassCode],[IsState] FROM [TeacherClass]";
		#endregion

		#region ITeacherClassProvider Members
		public Core.Business.TeacherClass Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectTeacherClass);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.TeacherClass teacherClass = new Core.Business.TeacherClass();

				if (!reader.IsDBNull(0)) teacherClass.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) teacherClass.TeacherCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) teacherClass.ClassCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) teacherClass.IsState = reader.GetBoolean(3);

				return teacherClass;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.TeacherClass teacherClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherClass.TeacherCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, teacherClass.ClassCode);
			sql.AddParameter("@IsState", SqlDbType.Bit, teacherClass.IsState);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertTeacherClass);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) teacherClass.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.TeacherClass teacherClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TeacherCode", SqlDbType.NVarChar, teacherClass.TeacherCode);
			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, teacherClass.ClassCode);
			sql.AddParameter("@IsState", SqlDbType.Bit, teacherClass.IsState);
			sql.AddParameter("@Id", SqlDbType.Int, teacherClass.Id);
			sql.ExecuteSql(SqlUpdateTeacherClass);
		}

		public void Delete(Core.Business.TeacherClass teacherClass)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, teacherClass.Id);
			sql.ExecuteSql(SqlDeleteTeacherClass);
		}

		public IList<Core.Business.TeacherClass> GetAllTeacherClass()
		{
			IList<Core.Business.TeacherClass> teacherClasslist = new List<Core.Business.TeacherClass>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllTeacherClass);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.TeacherClass teacherClass = new Core.Business.TeacherClass();

					if (!reader.IsDBNull(0)) teacherClass.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) teacherClass.TeacherCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) teacherClass.ClassCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) teacherClass.IsState = reader.GetBoolean(3);

					teacherClass.MarkOld();
					teacherClasslist.Add(teacherClass);
				}
				reader.Close();
			}
			return teacherClasslist;
		}
		#endregion
	}
}
